/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormTowary.java
 *
 * Created on 2010-01-21, 12:28:18
 */

package selling;

import java.io.*;
import java.util.ArrayList;
import wat.ziszcommon.clientgui.FunctionBase;
import wat.ziszcommon.clientgui.MyJTree;
import wat.ziszcommon.remote.RemoteModuleFactory;
import java.util.Date;
import javax.swing.JOptionPane;

/**
 *
 * @author Milewwski, Iwicki
 */
public class FormTowary extends FunctionBase<FormTowary.SellingModule> {

    @Override
    public void initClient() {
        System.out.println("TestModule");

        RefreshProductsList();
    }

    public void RefreshProductsList()
    {
        jComboBox1.removeAllItems();
        jComboBox1.addItem("<wybierz>");
        ArrayList<String> Towary = getServerObj().GetTowary();
        for (String towar : Towary) {
            String[] towarData = towar.split(",");
            jComboBox1.addItem(towarData[0]);
        }
    }
    
    public class Faktura {

    }

    public interface SellingModule extends FunctionBase.BaseServStub {
       public ArrayList<String> GetClients();
       public ArrayList<String> GetTowary();
       public ArrayList<String> GetFactures();
       public ArrayList<String> GetAccounts();
       public Boolean SaveTowary(ArrayList<String> Towary);
       public Boolean SaveClients(ArrayList<String> Klienci);
       public Boolean SaveFactures(ArrayList<String> Faktury);
       public Boolean SaveAccounts(ArrayList<String> Konta);
    }
    
    /** Creates new form FormTowary */
    public FormTowary(String name, MyJTree tree, FormTowary.SellingModule stub) {
        super(name, tree, stub);
        initComponents();

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Wybor(evt);
            }
        });

        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        jLabel1.setText("Towar:");

        jLabel2.setText("Cena towaru:");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jButton1.setText("Zapisz");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTextField2)
                    .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void Wybor(java.awt.event.ActionEvent evt) {
        if(jComboBox1.getSelectedItem() == null)
            return;

        if(jComboBox1.getSelectedItem().toString().compareTo("<wybierz>") == 0)
            {
                jTextField1.setText("");
                jTextField2.setText("");
                return;
            }

        ArrayList<String> Towary = getServerObj().GetTowary();
        for (String towar : Towary) {
            String[] towarData = towar.split(",");
     
            if(towarData[0].compareTo(jComboBox1.getSelectedItem().toString()) == 0)
            {
                jTextField1.setText(towarData[0]);
                jTextField2.setText(towarData[1]);
                break;
            }
        }
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
       if(jTextField1.getText().length() == 0)
            return;

       ArrayList<String> Towary = getServerObj().GetTowary();
       ArrayList<String> NewTowary = new ArrayList<String>();
       Boolean edit = false;
        for (String towar : Towary) {
            String[] towarData = towar.split(",");
            if(towarData[0].compareTo(jTextField1.getText()) == 0)
            {
                edit = true;
                NewTowary.add(towarData[0] + "," + jTextField2.getText());
            }
            else NewTowary.add(towar);
        }

        if(!edit) NewTowary.add(jTextField1.getText() + "," + jTextField2.getText());

        if(getServerObj().SaveTowary(NewTowary))
            if(!edit) JOptionPane.showMessageDialog(null, "Dodano produkt.");
            else JOptionPane.showMessageDialog(null, "Zmieniono produkt.");
        else
            JOptionPane.showMessageDialog(null, "Wystąpił błąd zapisu.");

        this.RefreshProductsList();
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables

}
